/*
 * @Author: cmd
 * @we_chat: cmd_develop
 * @Date: 2018-12-24 22:01:15
 * @LastEditors: cmd
 * @LastEditTime: 2019-03-20 22:45:22
 * @Description: 组件管理器
 * 
 */

const {ccclass, property} = cc._decorator;

@ccclass
export default class orc_alert{
    public static _alert_tips = false;
    public static _alert_loading = false;

    public static win_address = 'win/win_prefab';
    public static win_name = new Array();   

    public static preload(){
        orc.log('win preload'); 
        return new Promise((resolve,reject)=>{
            cc.loader.loadResDir(this.win_address,cc.Prefab,(err, prefab) => {  
                orc.log(prefab);              
                for(let i=0;i<prefab.length;i++)
                {                
                    this.win_name[prefab[i].name] = prefab[i];
                }    
                resolve('ok');            
            });
        });
    }

    public static show_alert_tips(text:string){       
        var btn_temp = this._alert_tips;
        let temp_node = cc.find("Canvas").getChildByName('alert_tips');          
        if (temp_node != undefined) { 
            let script = temp_node.getComponent("component_alert");
            script.show_tips(text); 
        } 
        else {            
            if (btn_temp) {
                return;
            }            
            btn_temp = true;
            let temp_node = cc.instantiate(this.win_name['alert_tips']);      
            cc.find("Canvas").addChild(temp_node, 101, 'alert_tips');  
            let script = temp_node.getComponent("component_alert");
            script.show_tips(text);                      
            btn_temp = false;
            
        }
    }

    public static show_alert_loading(text:string){
        return new Promise((reslove)=>{
            var btn_temp = this._alert_loading;
            let temp_node = cc.find("Canvas").getChildByName('alert_loading');               
            if (temp_node != undefined) { 
                let script = temp_node.getComponent("component_alert");
                script.show_loading(text); 
                reslove('ok')
            } 
            else {            
                if (btn_temp) {
                    return;
                }            
                btn_temp = true;  
               
                console.log('加载alert_loading');       
                let temp_node = cc.instantiate(this.win_name['alert_loading']);                
                cc.find("Canvas").addChild(temp_node, 100, 'alert_loading');  
                let script = temp_node.getComponent("component_alert");
                script.show_loading(text);                      
                btn_temp = false;
                reslove('ok')
               
            }
        })
        
    }

    public static close_alert_loading(){ 
        let temp_node = cc.find("Canvas").getChildByName('alert_loading');        
        if (temp_node != undefined) { 
            let script = temp_node.getComponent("component_alert");
            script.close_loading(); 
        } 
        else { 
            console.log('temp_node:',temp_node)
        }
    }

    /**
     * 
     * @param win_name 窗口名字     
     */
    public static win_open(win_name:string,param?:any){
        //await orc.component_manager.show_alert_loading('加载中');
        //orc.sound_manager.play_effect('button');
        let win_path:string = 'win/' + win_name;
        let temp_node = cc.find("Canvas").getChildByName(win_name);       
        if (temp_node === null) {            
            let temp_node = cc.instantiate(this.win_name[win_name]);
            if(param)
            {
                temp_node.getComponent(win_name).param = param;
            }
            let scene_size = cc.view.getVisibleSize();
            temp_node.width = scene_size.width;
            temp_node.height = scene_size.height;                   
            cc.find("Canvas").addChild(temp_node,99,win_name);
            
        } 
        else {   
            temp_node.active = true;
        }
    }

    /**
     * 
     * @param win_name 窗口名字
     * @param close_type 窗口以何种模式关闭
     */
    public static win_close(win_name:string){
        //orc.sound_manager.play_effect('button');
        let temp_node = cc.find("Canvas").getChildByName(win_name); 
        if (temp_node === null) { 
            console.log('窗口名字不存在')
        } 
        else {   
            temp_node.active = false;
        }
    }
}
